package com.ttg.web.service.Impl;

import com.ttg.common.constant.BasicConstant;
import com.ttg.common.utils.DateTimeUtil;
import com.ttg.model.dto.function.MobileSmsDTO;
import com.ttg.model.pojo.Admin;
import com.ttg.web.dao.AdminDao;
import com.ttg.web.service.FunctionService;
import com.ttg.web.service.TaskWarnNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class TaskWarnNoticeServiceImpl implements TaskWarnNoticeService {


    @Autowired
    private FunctionService functionService;

    @Autowired
    private AdminDao adminDao;


    @Value("#{'${task.warnNotice}'.split(',')}")
    private List<Long> warnNotice;

    @Value("${sysZhName:贡献度系统-}")
    private String sysZhName;

    @Override
    public void errorReport(String taskName) {
        List<Admin> admins = adminDao.getAdmins(warnNotice, BasicConstant.INT_1);
        if (CollectionUtils.isEmpty(admins)){ return; }

        String dateTime = DateTimeUtil.localDateTimeToYYYY_MM_DD(LocalDateTime.now());
        for (Admin admin : admins){
            if (StringUtils.isBlank(admin.getAdmMobile())){
                continue;
            }
            MobileSmsDTO mobileSmsDTO = new MobileSmsDTO(
                    2,
                    admin.getAdmMobile(),
                    dateTime + sysZhName + taskName + "执行失败",
                    LocalDateTime.now().plusSeconds(600),
                    null,
                    true,
                    false,
                    null,
                    null,
                    false
            );
            functionService.mobileSms(mobileSmsDTO);
        }
    }

}
